<?php
class weixin extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览微信信息', '1-2');
		$da = $this -> pub -> get_setting('微信信息', 'Weixin');
		$this -> load -> view('adminx/weixin', $da);
	}

	function get() {
		$this -> pub -> logp('获取微信信息信息', '1-2');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;

		$this -> db -> where('CreateTime >', $day1);
		$this -> db -> where('CreateTime <', $day2);

		if ($this -> input -> post('openid')) {
			$this -> db -> where('FromUserName', $this -> input -> post('openid'));
		}

		$this -> db -> where('ToUserName !=', '1');
		if ($this -> input -> post('key')) {
			$this -> db -> select('count(*)');
			$this -> db -> where('FromUserName in(select openid from (select openid from pan_member where  nickname like \'%' . $this -> input -> post('key') . '%\') as tbt )');
			if ($this -> input -> post('state') > 0) {
				$this -> db -> where('pan_wx.state', $this -> input -> post('state'));
			}
			if ($this -> input -> post('MsgType')) {
				$this -> db -> where('MsgType', $this -> input -> post('MsgType'));
			}
			$q = $this -> db -> get_where('wx');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
			$this -> db -> where('FromUserName in(select openid from (select openid from pan_member where  nickname like \'%' . $this -> input -> post('key') . '%\') as tbt )');
		} else {
			if ($this -> input -> post('state') > 0) {
				$this -> db -> where('pan_wx.state', $this -> input -> post('state'));
			}
			if ($this -> input -> post('MsgType')) {
				$this -> db -> where('MsgType', $this -> input -> post('MsgType'));
			}
			$this -> db -> select('count(*)');
			$q = $this -> db -> get_where('wx');
			$ls = $q -> row_array(0);
			$num = $ls['count(*)'];
		}

		//一共的页数
		$da['num'] = ceil($num / 30);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(30, $page * 30);

		$this -> db -> where('ToUserName !=', '1');
		$this -> db -> order_by('wx_id', 'desc');

		if ($this -> input -> post('state') > 0) {
			$this -> db -> where('pan_wx.state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('MsgType')) {
			$this -> db -> where('MsgType', $this -> input -> post('MsgType'));
		}

		$this -> db -> where('CreateTime >', $day1);
		$this -> db -> where('CreateTime <', $day2);

		if ($this -> input -> post('openid')) {
			$this -> db -> where('FromUserName', $this -> input -> post('openid'));
		}
		$q = $this -> db -> get_where('wx');
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			//获取个人信息
			$qq = $this -> db -> get_where('member', array('openid' => $v['FromUserName']));
			$m = $qq -> row_array(0);
			$da['content'] .= '<td><a target="_blank" href="/iadmin/member/info/' . $m['mid'] . '"><img width="48" src="' . ($m['headimgurl'] ? $m['headimgurl'] : '/mex/lance/img/tx.jpg') . '" /></a></td>';
			$da['content'] .= '<td style="width: 100px;word-break:break-all; word-wrap:break-word;">' . $m['nickname'] . '  &nbsp; &nbsp; &nbsp;' . ($m['sex'] == 1 ? '男' : '女') . '</td>';
			$da['content'] .= '<td>' . $v['MsgType'] . '</td>';
			$content = unserialize($v['content']);
			switch($v['MsgType']) {
				case 'text' :
					$da['content'] .= '<td style="width: 280px;word-break:break-all; word-wrap:break-word;">' . $content['Content'] . '</td>';
					break;
				case 'voice' :
					$da['content'] .= '<td>' . $content['Recognition'] . '</td>';
					break;
				case 'image' :
					$da['content'] .= '<td><img class="img-responsive" src="' . $content['PicUrl'] . '" /></td>';
					break;
				case 'location' :
					$da['content'] .= '<td>坐标：' . $content['Location_X'] . ',' . $content['Location_Y'] . ' | ' . '地图放缩：' . $content['Scale'] . ' | ' . $content['Label'] . '</td>';
					break;
				case 'link' :
					$da['content'] .= '<td><a href="' . $content['Url'] . '">' . $content['Title'] . '</a><br />' . $content['Description'] . '</td>';
					break;
				case 'event' :
					$da['content'] .= '<td>' . $content['Event'] . ' | ' . $content['EventKey'] . '</td>';
					break;
			}
			$da['content'] .= '<td>' . ($v['statex'] == 1 ? '未处理' : '已处理') . '</td>';
			$da['content'] .= '<td>' . date('Y-m-d H:i', $v['CreateTime']) . '</td>';
			$da['content'] .= '<td><button onclick="show_wx_ks(\'' . $v['FromUserName'] . '\',\'' . $m['nickname'] . '\')" type="button" class="btn blue"><i class="fa fa-weixin"></i> 快速回复</button>  </td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function wx_hf() {
		$this -> pub -> logp('快速回复微信信息 - ' . $this -> input -> post('idx'), '1-2');
		if ($this -> input -> post('content')) {
			$da['FromUserName'] = $this -> input -> post('idx');
			$da['ToUserName'] = 1;
			$da['CreateTime'] = mktime();
			$da['MsgType'] = 'text';
			$da['MsgId'] = $this -> session -> userdata('sid');
			$co['Content'] = $this -> input -> post('content');
			$da['content'] = serialize($co);
			$this -> db -> insert('wx', $da);
			$this -> pub -> send_zd_text($this -> input -> post('idx'), $this -> input -> post('content'));
			$this -> db -> update('wx', array('state' => 2), array('FromUserName' => $this -> input -> post('idx')));
			echo json_encode(array('type' => 1));
		} else {
			echo json_encode(array('type' => 2));
		}
	}

	function wx_hf_image() {
		$this -> pub -> logp('快速回复微信信息图片 - ' . $this -> input -> post('idx'), '1-2');
		if ($this -> input -> post('content')) {
			$da['FromUserName'] = $this -> input -> post('idx');
			$da['ToUserName'] = 1;
			$da['CreateTime'] = mktime();
			$da['MsgType'] = 'text';
			$da['MsgId'] = $this -> session -> userdata('sid');
			$co['Content'] = $this -> input -> post('content');
			$da['content'] = serialize($co);
			$this -> db -> insert('wx', $da);

			$this -> pub -> send_zd_text($this -> input -> post('idx'), $this -> input -> post('content'));
			$this -> db -> update('wx', array('state' => 2), array('FromUserName' => $this -> input -> post('idx')));
			echo json_encode(array('type' => 1));
		} else {
			echo json_encode(array('type' => 2));
		}
	}

	//获取聊天信息
	function get_wx_lt() {
		$this -> pub -> logp('获取聊天信息 - ' . $this -> input -> post('idx'), '1-2');
		if ($this -> input -> post('state') > 0) {
			$this -> db -> where('state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('MsgType')) {
			$this -> db -> where('MsgType', $this -> input -> post('MsgType'));
		}
		$this -> db -> select('count(*)');
		$q = $this -> db -> get_where('wx', array('FromUserName' => $this -> input -> post('idx')));
		$ls = $q -> row_array(0);
		$num = $ls['count(*)'];

		//一共的页数
		$da['num'] = ceil($num / 10);

		$page = is_numeric($this -> input -> post('page')) && $this -> input -> post('page') > 0 ? $this -> input -> post('page') : 1;
		$page = $page > $da['num'] ? $da['num'] : $page;
		$da['page'] = $page;

		$page = ($page - 1) == -1 ? 0 : ($page - 1);

		$this -> db -> limit(10, $page * 10);

		$this -> db -> order_by('wx_id', 'desc');

		if ($this -> input -> post('state') > 0) {
			$this -> db -> where('state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('MsgType')) {
			$this -> db -> where('MsgType', $this -> input -> post('MsgType'));
		}

		$this -> db -> join('member', 'pan_member.openid=pan_wx.FromUserName');
		$q = $this -> db -> get_where('wx', array('FromUserName' => $this -> input -> post('idx')));
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td><a target="_blank" href="/sgl/tx/' . $v['mid'] . '"><img width="48" src="' . ($v['ToUserName'] == 1 ? '/style/images/logo.png' : $v['headimgurl']) . '" /></a></td>';
			$da['content'] .= '<td>' . ($v['ToUserName'] == 1 ? $this -> config -> item('xt_name') : $v['nickname']) . '</td>';
			$da['content'] .= '<td>' . $v['MsgType'] . '</td>';
			$content = unserialize($v['content']);
			switch($v['MsgType']) {
				case 'text' :
					$da['content'] .= '<td>' . $content['Content'] . '</td>';
					break;
				case 'voice' :
					$da['content'] .= '<td>' . $content['Recognition'] . '</td>';
					break;
				case 'image' :
					$da['content'] .= '<td><img class="img-responsive" src="' . $content['PicUrl'] . '" /></td>';
					break;
				case 'location' :
					$da['content'] .= '<td>坐标：' . $content['Location_X'] . ',' . $content['Location_Y'] . ' | ' . '地图放缩：' . $content['Scale'] . ' | ' . $content['Label'] . '</td>';
					break;
				case 'link' :
					$da['content'] .= '<td><a href="' . $content['Url'] . '">' . $content['Title'] . '</a><br />' . $content['Description'] . '</td>';
					break;
				case 'event' :
					$da['content'] .= '<td>' . $content['Event'] . ' | ' . $content['EventKey'] . '</td>';
					break;
			}

			$da['content'] .= '<td>' . date('Y-m-d H:i', $v['CreateTime']) . '</td>';
			$da['content'] .= '<td>' . ($v['state'] == 1 ? '未处理' : '已处理') . '</td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>